#include<iostream>
#include<algorithm>
using namespace std;
const int N = 500*500+10;
struct node
{
	int x;
	int y;
	int z;
}a[N];
int A, B;
int pos;
int fa[N];
int ret, cnt;
bool cmp(node& a, node& b)
{
	return a.z < b.z;
}
int find(int x)
{
	if (x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
void kk()
{
	sort(a + 1, a + 1 + pos,cmp);//注意这里也是加pos!!!
	for (int i = 1;i <= pos;i++)//一定注意这遍历的是边的个数不是节点的个数
	{
		int x = a[i].x;
		int y = a[i].y;
		int z = a[i].z;
		int fx = find(x);int fy = find(y);
		if (fx != fy)
		{
			ret += z;
			cnt++;
			fa[fx] = fy;
		}
	}
}
int main()
{
	cin >> A >> B;
	for (int i = 1;i <= B;i++)
	{
		for (int j = 1;j <= B;j++)
		{
			int x=0;
			cin >> x;
			if (x == 0 || i >= j||x>A) continue;//一定要注意有三种情况
			pos++;
			a[pos].x = i;a[pos].y = j;a[pos].z = x;
		}
	}
	//初始化
	for (int i = 1;i <= B;i++) fa[i] = i;
	kk();

	cout << ret + (B- cnt) * A << endl;
	return 0;
}